package com.xfatm.killer.entity;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xfatm.killer.R;
import com.xfatm.killer.util.Activitys;
import com.xfatm.killer.util.CommandExecution;

import java.util.List;
import java.util.Set;


/**
 * Created by wenjie on 2018/3/24.
 */

public class AppInfoAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{

    private List<AppInfo> appInfos;
    private Context context;

    public AppInfoAdapter(Context context,List<AppInfo> data){
        this.context=context;
        this.appInfos=data;
    }

    @Override
    public int getCount() {
        return appInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return appInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return appInfos.get(position).hashCode();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.app_info_item,parent,false);
            holder = new ViewHolder();
            holder.appName = (TextView) convertView.findViewById(R.id.appName);
            holder.packageName=convertView.findViewById(R.id.packageName);
            holder.icon=convertView.findViewById(R.id.appIcon);
            convertView.setTag(holder);   //将Holder存储到convertView中
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.appName.setText(appInfos.get(position).getAppName());
        holder.packageName.setText(appInfos.get(position).getPackageName());
        holder.icon.setImageDrawable(appInfos.get(position).getIcon());
        if(appInfos.get(position).isChecked()){
            Log.i("sout",appInfos.get(position).getAppName());
            convertView.setBackgroundColor(convertView.getContext().getResources().getColor(R.color.checkBG));
        }else{
            convertView.setBackgroundColor(parent.getDrawingCacheBackgroundColor());
        }
        return convertView;
    }

    static class ViewHolder{
        TextView appName;
        TextView packageName;
        ImageView icon;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AppInfo appInfo=appInfos.get(position);
        /*if(appInfo.isChecked()){
            view.setBackgroundColor(parent.getDrawingCacheBackgroundColor());
        }else{
            view.setBackgroundColor(view.getContext().getResources().getColor(R.color.checkBG));
        }*/
        appInfo.setChecked(!appInfo.isChecked());
        Set<String> sets=AppInfoController.getInstance().getSelectAppSet();
        if(appInfo.isChecked()){
            AppInfoController.getInstance().addSelectApp(appInfo);
            int result=AppInfoController.getInstance().disableApp(appInfo);
            if(result==0){
                Toast.makeText(Activitys.top(),"已禁用",Toast.LENGTH_SHORT).show();
            }
        }else{
            AppInfoController.getInstance().removeSelectApp(appInfo);
            AppInfoController.getInstance().enableApp(appInfo);
        }
        AppInfoController.getInstance().getSelectAppInfoAdapter().notifyDataSetChanged();
        notifyDataSetChanged();
    }
}
